package io.github.source.media.bean;


import com.google.gson.annotations.SerializedName;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.room.PrimaryKey;

/**
 * @ClassName: MediaQuery
 * @Description: (Media多媒体的查询, 请求 ， 头信息 ， 协议 ， 解析协议 ， 请求协议等)
 * 可通过创建函数Builder的提供的方案，也可以自定义Map参数据
 * @author: huangrongcai
 * @date: 2024/6/3
 * @Copyright: 黄嵘才（HuangRc)
 */
public class MediaQuery implements IMediaQuery, Serializable {
    public final static int COUNT_NUM = 20;
    /**首页推荐媒体*/
    public static final String TYPE_HOME = "Home";
    public static final String TYPE_DETAILS = "Details";
    public static final String TYPE_PLAYBACK = "Playback";
    public static final String TYPE_SEARCH = "Search";
    public static final String TYPE_HOT_VOD = "HotVod";
    public HashMap<String, Object> queryMap = new HashMap<>();
    public static final Object sPoolSync = new Object();
    private static MediaQuery sPool;
    private static int sPoolSize = 0;

    private static final int MAX_POOL_SIZE = 50;
    private int flags;
    private MediaQuery next;
    static final int FLAG_IN_USE = 1 << 0;

    /*package*/ boolean isInUse() {
        return ((flags & FLAG_IN_USE) == FLAG_IN_USE);
    }

    /*package*/ void markInUse() {
        flags |= FLAG_IN_USE;
    }

    /**
     * TODO 享元模式 共享对象
     *
     * @return
     */
    public static MediaQuery obtain() {
        synchronized (sPoolSync) {
            if (sPool != null) {
                MediaQuery m = sPool;
                sPool = m.next;
                m.next = null;
                m.flags = 0; // clear in-use flag
                sPoolSize--;
                return m;
            }
        }

        return new MediaQuery();
    }

    public void clear() {
        queryMap.clear();
    }

    /**
     * 源站点
     */
    @NonNull
    @PrimaryKey
    @SerializedName("site_key")
    public String siteKey;
    /**
     * 分类ID(type_id)
     */
    @SerializedName("type_id")
    private String id;
    /**
     * 偏移位起始位置
     */
    @SerializedName("offset")
    private int offset = 0;
    /**
     * 取多少条数据（默认20条）
     */
    @SerializedName("count")
    private int count = COUNT_NUM;
    @SerializedName("count")
    private Map<String, String> extend;

    /**
     * 过滤
     */
    public boolean filter;
    /**
     * 搜索关键词
     */
    @SerializedName("keyword")
    public String keyword;
    @SerializedName("quick")
    public boolean quick;
    public String type;

    /**
     * 播放地址(它可能不是真正的可播地址，可能需要进一步检查，请求，解析，转换)
     */
    @SerializedName("play_url")
    public String playUrl;
    @SerializedName("play_flag")
    public String playFlag;

    public MediaQuery() {

    }

    public MediaQuery put(String key, Object value) {
        queryMap.put(key, value);
        return this;
    }

    public static MediaQuery create() {
        return new MediaQuery();
    }

    public static MediaQuery create(String type) {
        MediaQuery query = new MediaQuery();
        query.type = type;
        return query;
    }

    public MediaQuery(Builder builder) {
        setId(builder.id);
        setType(builder.type);
        setSiteKey(builder.siteKey);
        setKeyword(builder.keyWord);
        setPlayUrl(builder.playUrl);
        setPlayFlag(builder.playFlag);
        page(builder.page);
    }

    public static Builder createBuilder() {
        return new Builder();
    }

    public int page() {
        int page = ((offset + count) / COUNT_NUM);
        return page;

    }

    public void page(int page) {
        this.offset = (page - 1) * COUNT_NUM;
    }


    public String getSiteKey() {
        return siteKey;
    }

    public void setSiteKey(@NonNull String key) {
        this.siteKey = key;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public boolean isFilter() {
        return filter;
    }

    public void setFilter(boolean filter) {
        this.filter = filter;
    }

    public Map<String, String> getExtend() {
        return extend;
    }

    public void setExtend(Map<String, String> extend) {
        this.extend = extend;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public boolean isQuick() {
        return quick;
    }

    public void setQuick(boolean quick) {
        this.quick = quick;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPlayUrl() {
        return playUrl;
    }

    public void setPlayUrl(String playUrl) {
        this.playUrl = playUrl;
    }

    public String getPlayFlag() {
        return playFlag;
    }

    public void setPlayFlag(String playFlag) {
        this.playFlag = playFlag;
    }

    public Map<String, String> toMap() {
        return new HashMap<>();
    }

    public List<String> toList() {
        return new ArrayList<>();
    }

    @Override
    public String toString() {
        return "MediaQuery{" + "siteKey='" + siteKey + '\'' + ", id='" + id + '\'' + ", offset=" + offset + ", count=" + count + ", extend=" + extend + ", filter=" + filter + ", keyword='" + keyword + '\'' + ", quick=" + quick + ", type='" + type + '\'' + ", playUrl='" + playUrl + '\'' + ", playFlag='" + playFlag + '\'' + '}';
    }

    public static class Builder {
        private String id;
        private String type;
        private String keyWord;
        private String siteKey;
        public String playUrl;
        public String playFlag;
        private long time;
        private int page;

        public Builder() {
            /*默认配置*/
            this.page = 1;
            this.siteKey = "";

        }

        public MediaQuery build() {
            return new MediaQuery(this);
        }

        public Builder setTime(long time) {
            this.time = time;
            return this;
        }

        public Builder setType(String type) {
            this.type = type;
            return this;
        }

        public Builder setKeyword(String keyWord) {
            this.keyWord = keyWord;
            return this;
        }

        /**
         * 源站点ID
         *
         * @param key 源站点ID 参考{@link io.github.source.bean.Site}
         * @return
         */
        public Builder setSiteKey(String key) {
            this.siteKey = key;
            return this;
        }

        public Builder setPlayFlag(String playFlag) {
            this.playFlag = playFlag;
            return this;
        }

        public Builder setPlayUrl(String playUrl) {
            this.playUrl = playUrl;
            return this;
        }

        /**
         * 分类ID
         *
         * @param id 分类ID
         * @return {@link Builder}
         */
        public Builder setId(String id) {
            this.id = id;
            return this;
        }

        /**
         * 分页
         *
         * @param page 分类页
         * @return {@link Builder}
         */
        public Builder page(int page) {
            this.page = page;
            return this;
        }
    }

}
